﻿<?php

class Familymember_IndexController extends Core_Controller_Action_User {
	public function indexAction() {
		$this->view->form = $form = new Familymember_Form_Add ();
		
		if (! $this->getRequest ()->isPost ()) {
			return;
		}
		
		if (! $form->isValid ( $this->getRequest ()->getPost () )) {
			return;
		}
		
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		
		$table = Engine_Api::_ ()->getDbTable ( 'member', 'familymember' );
		$db = $table->getAdapter ();
		$db->beginTransaction ();
		
		try {
			for($i = 0; $i < 5; $i ++) {
				$email = $this->_getParam ( 'email' . $i );
				$user = Engine_Api::_ ()->user ()->getUser ( $email );
				$relation = $this->_getParam ( 'relation' . $i );
				if (! $user->getIdentity () && ! $relation) {
					// CONTINUE
				} else if (! $user->getIdentity () || ! $relation) {
					$form->addError ( Zend_Registry::get ( 'Zend_Translate' )->_ ( '错误的用户名或关系选择有误.' ) );
				} else if (! $viewer->membership ()->isMember ( $user )) {
					$form->addError ( $user->getTitle () . Zend_Registry::get ( 'Zend_Translate' )->_ ( ' 无法添加为您的家庭成员.' ) );
				} else if (Engine_Api::_ ()->getDbTable ( 'member', 'familymember' )->fetchRow ( "resource_id = " . $viewer->getIdentity () . " AND user_id = " . $user->getIdentity () )) {
					$form->addError ( $user->getTitle () . Zend_Registry::get ( 'Zend_Translate' )->_ ( ' 已经是家庭中的一员(或已经发送了请求).' ) );
				} else {
					$row = $table->createRow ();
					$row->resource_id = $viewer->getIdentity ();
					$row->user_id = $user->getIdentity ();
					$row->type_id = $relation;
					$row->approved = 1;
					$row->save ();
					
					$row = $table->createRow ();
					$row->resource_id = $user->getIdentity ();
					$row->user_id = $viewer->getIdentity ();
					$row->type_id = '';
					$row->approved = 0;
					$row->save ();
					
					$form->addNotice ( Zend_Registry::get ( 'Zend_Translate' )->_ ( '发送家庭成员邀请给 ' ) . $user->getTitle () );
					
					Engine_Api::_ ()->getDbtable ( 'notifications', 'activity' )->addNotification ( $user, $viewer, $user, 'family_request' );
				}
			}
			
			$db->commit ();
		} catch ( Exception $e ) {
			throw $e;
		}
	}
	
	public function addAction() {
		$this->view->form = $form = new Familymember_Form_Edit ();
		$form->removeElement ( 'delete' );
		$form->setTitle ( 'Add Family Member' );
		
		if (! $this->getRequest ()->isPost ()) {
			$this->view->status = false;
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( '没有执行操作' );
			return;
		}
		
		if (! $form->isValid ( $this->getRequest ()->getPost () )) {
			$this->view->status = false;
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( '无效数据' );
			return;
		}
		
		$id = $this->_getParam ( 'id', null );
		$user = Engine_Api::_ ()->user ()->getUser ( $id );
		if (null == $id || ! $user->getIdentity ()) {
			$form->addError ( Zend_Registry::get ( 'Zend_Translate' )->_ ( '错误的用户名或关系选择有误.' ) );
		}
		
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		
		$table = Engine_Api::_ ()->getDbTable ( 'member', 'familymember' );
		$db = $table->getAdapter ();
		$db->beginTransaction ();
		
		try {
			$relation = $this->_getParam ( 'relation', null );
			$type = $this->_getParam ( 'relation', null );
			if (! $user->getIdentity ()) {
				$form->addError ( Zend_Registry::get ( 'Zend_Translate' )->_ ( '错误的用户名或关系选择有误.' ) );
			} else if (! $viewer->membership ()->isMember ( $user )) {
				$form->addError ( $user->getTitle () . Zend_Registry::get ( 'Zend_Translate' )->_ ( ' 无法添加为您的家庭成员.' ) );
			} else if (Engine_Api::_ ()->getDbTable ( 'member', 'familymember' )->fetchRow ( "resource_id = " . $viewer->getIdentity () . " AND user_id = " . $user->getIdentity () )) {
				$form->addError ( $user->getTitle () . Zend_Registry::get ( 'Zend_Translate' )->_ ( ' 已经是家庭中的一员(或已经发送了请求).' ) );
			}
			if (null == $relation) {
				$this->view->status = false;
				$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( '没有指定的关系类型' );
				$form->addError ( Zend_Registry::get ( 'Zend_Translate' )->_ ( '没有指定的关系类型' ) );
			} else {
				$row = $table->createRow ();
				$row->resource_id = $viewer->getIdentity ();
				$row->user_id = $user->getIdentity ();
				$row->type_id = $relation;
				$row->approved = 1;
				$row->save ();
				
				$row = $table->createRow ();
				$row->resource_id = $user->getIdentity ();
				$row->user_id = $viewer->getIdentity ();
				$row->type_id = '';
				$row->approved = 0;
				$row->save ();
				
				Engine_Api::_ ()->getDbtable ( 'notifications', 'activity' )->addNotification ( $user, $viewer, $user, 'family_request' );
			}
			
			$db->commit ();
			
			$this->view->status = true;
			$this->view->message = Zend_Registry::get ( 'Zend_Translate' )->_ ( '发送家庭成员请求给 ' ) . $user->getTitle ();
			
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( '发送家庭成员请求给 ' ) . $user->getTitle () ) ) );
		} catch ( Exception $e ) {
			echo $e->getMessage ();
		}
	
	}
	
	public function editAction() {
		$id = $this->_getParam ( 'id', null );
		if (null == $id) {
			$this->view->status = false;
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( '无效数据' );
			return;
		}
		
		$this->view->form = $form = new Familymember_Form_Edit ();
		
		if (! $this->getRequest ()->isPost ()) {
			$this->view->status = false;
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( '没有执行操作' );
			return;
		}
		
		if (! $form->isValid ( $this->getRequest ()->getPost () )) {
			$this->view->status = false;
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( '无效数据' );
			return;
		}
		
		$type = $this->_getParam ( 'relation', null );
		if (null == $type) {
			$this->view->status = false;
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( '没有指定的关系类型' );
			return $form->addError ( Zend_Registry::get ( 'Zend_Translate' )->_ ( '没有指定的关系类型' ) );
			return;
		}
		
		// Process
		$table = Engine_Api::_ ()->getDbtable ( 'member', 'familymember' );
		$db = $table->getAdapter ();
		$db->beginTransaction ();
		
		try {
			if ($this->_getParam ( 'delete', null )) {
				$relation = $table->fetchRow ( "member_id = " . $id );
				$reverse = $table->fetchRow ( "resource_id = " . $relation->user_id . " AND user_id = " . $relation->resource_id );
				$relation->delete ();
				$reverse->delete ();
			} else if (is_numeric ( $type )) {
				$relation = $table->fetchRow ( "member_id = " . $id );
				$relation->type_id = $type;
				$relation->save ();
			}
			
			$db->commit ();
			
			$this->view->status = true;
			$this->view->message = Zend_Registry::get ( 'Zend_Translate' )->_ ( '家庭关系类型更新.' );
			
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( '家庭关系类型更新.' ) ) ) );
		} catch ( Exception $e ) {
			$db->rollBack ();
			
			$this->view->status = false;
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( '系统错误.' );
			$this->view->exception = $e->__toString ();
		}
	}
	
	public function requestsAction() {
		$this->view->notification = $notification = $this->_getParam ( 'notification' );
		$row = Engine_Api::_ ()->getDbTable ( 'member', 'familymember' )->fetchRow ( "resource_id = " . $notification->getSubject ()->getIdentity () . " AND user_id = " . $notification->getObject ()->getIdentity () );
		$this->view->types = $types = Engine_Api::_ ()->getDbTable ( 'type', 'familymember' )->fetchAll ();
		$this->view->type = Engine_Api::_ ()->getDbTable ( 'type', 'familymember' )->fetchRow ( "type_id = " . $row->type_id )->title;
	}
	
	public function confirmAction() {
		// Get viewer and other user
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		if (null == ($user_id = $this->_getParam ( 'user_id' )) || null == ($user = Engine_Api::_ ()->getItem ( 'user', $user_id ))) {
			$this->view->status = false;
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( '没有指定的会员' );
			return;
		}
		
		if (! $this->getRequest ()->isPost ()) {
			$this->view->status = false;
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( '没有执行操作' );
			return;
		}
		
		$row = Engine_Api::_ ()->getDbTable ( 'member', 'familymember' )->fetchRow ( "resource_id = " . $viewer->getIdentity () . " AND user_id = " . $user->getIdentity () );
		if ($row->approved) {
			$this->view->status = false;
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( '已经是家庭成员' );
			return;
		}
		
		// Process
		$table = Engine_Api::_ ()->getDbTable ( 'member', 'familymember' );
		$db = $table->getAdapter ();
		$db->beginTransaction ();
		
		try {
			$row = Engine_Api::_ ()->getDbTable ( 'member', 'familymember' )->fetchRow ( "resource_id = " . $viewer->getIdentity () . " AND user_id = " . $user->getIdentity () );
			$row->approved = 1;
			$row->type_id = $this->_getParam ( 'type' );
			$row->save ();
			
			// Add activity
			Engine_Api::_ ()->getDbtable ( 'actions', 'activity' )->addActivity ( $user, $viewer, 'new_family', '', array ('label' => str_replace ( '_', ' ', Engine_Api::_ ()->getDbTable ( 'type', 'familymember' )->fetchRow ( "type_id = " . $this->_getParam ( 'type' ) )->title ) ) );
			
			// Set the requests as handled
			$notification = Engine_Api::_ ()->getDbtable ( 'notifications', 'activity' )->getNotificationBySubjectAndType ( $viewer, $user, 'family_request' );
			if ($notification) {
				$notification->mitigated = true;
				$notification->read = 1;
				$notification->save ();
			}
			
			$db->commit ();
			
			$message = Zend_Registry::get ( 'Zend_Translate' )->_ ( '您现在已经是 %s 家庭中的一员' );
			$message = sprintf ( $message, $user->__toString () );
			
			$this->view->status = true;
			$this->view->message = $message;
			return $this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'messages' => array ($message ) )//'You have accepted this friend request.'
			

			 );
		} catch ( Exception $e ) {
			$db->rollBack ();
			
			$this->view->status = false;
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( '系统错误.' );
			$this->view->exception = $e->__toString ();
		}
	}
	
	public function rejectAction() {
		// Get viewer and other user
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		if (null == ($user_id = $this->_getParam ( 'user_id' )) || null == ($user = Engine_Api::_ ()->getItem ( 'user', $user_id ))) {
			$this->view->status = false;
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( '没有指定的会员' );
			return;
		}
		
		if (! $this->getRequest ()->isPost ()) {
			$this->view->status = false;
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( '没有执行操作' );
			return;
		}
		
		// Process
		$table = Engine_Api::_ ()->getDbTable ( 'member', 'familymember' );
		$db = $table->getAdapter ();
		$db->beginTransaction ();
		
		try {
			$row = Engine_Api::_ ()->getDbTable ( 'member', 'familymember' )->fetchRow ( "resource_id = " . $viewer->getIdentity () . " AND user_id = " . $user->getIdentity () );
			$row->delete ();
			$row = Engine_Api::_ ()->getDbTable ( 'member', 'familymember' )->fetchRow ( "resource_id = " . $user->getIdentity () . " AND user_id = " . $viewer->getIdentity () );
			$row->delete ();
			
			// Set the request as handled
			$notification = Engine_Api::_ ()->getDbtable ( 'notifications', 'activity' )->getNotificationBySubjectAndType ( $viewer, $user, 'family_request' );
			if ($notification) {
				$notification->mitigated = true;
				$notification->read = 1;
				$notification->save ();
			}
			
			$db->commit ();
			
			$message = Zend_Registry::get ( 'Zend_Translate' )->_ ( '您忽略了 %s 的家庭成员请求' );
			$message = sprintf ( $message, $user->__toString () );
			
			$this->view->status = true;
			$this->view->message = $message;
			return $this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'messages' => array ($message ) ) );
		} catch ( Exception $e ) {
			$db->rollBack ();
			
			$this->view->status = false;
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( '出现系统错误,请重试.' );
			$this->view->exception = $e->__toString ();
		}
	}

}
